<template>
  <el-dialog
    v-model="visible"
    v-if="load"
    :title="form.id ? '修改品牌' : '新增'"
    width="600"
    draggable
    @close="close"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :z-index="2000"
  >
    <el-form
      ref="formRef"
      :model="form"
      :rules="rules"
      label-align="right"
      label-width="100"
      size="default"
      label-position="top"
    >
      <el-row :gutter="20">
        <el-col :span="21">
          <el-form-item label="品牌名称" prop="name">
            <el-input v-model.trim="form.name" placeholder="请输入" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="3" class="mt-7">
          <el-form-item label="" label-width="0" prop="status">
            <el-switch
              v-model="form.status"
              :active-value="1"
              :inactive-value="2"
              active-text="启用"
              inactive-text="禁用"
              inline-prompt
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="品牌中文名" prop="alias">
            <el-input v-model.trim="form.alias" placeholder="请输入" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="品牌类型" prop="type">
            <el-select v-model="form.type" clearable placeholder="请选择" class="w-full">
              <el-option
                v-for="item in options.typeList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="报关品牌类型">
            <el-select
              v-model="form.customs_type"
              clearable
              placeholder="请选择"
              class="w-full"
            >
              <el-option
                v-for="item in options.customs_type"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="品牌logo" prop="img">
        <UploadImg v-model:imageUrl="form.img" width="88px" height="88px" :file-size="2">
        </UploadImg>
      </el-form-item>
      <el-form-item label="备注信息">
        <el-input v-model.trim="form.desc" placeholder="请输入" type="textarea" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" :loading="loading" @click="save(formRef)"
        >保 存</el-button
      >
    </template>
  </el-dialog>
</template>
<script setup name="ComModal">
import api from "../api";
import UploadImg from "@/components/UploadImg/index.vue";
import { reactive, toRefs, getCurrentInstance } from "vue";
import { ElMessage } from "element-plus";
const { proxy } = getCurrentInstance();
const emits = defineEmits(["query"]);
const props = defineProps({
  options: {
    type: Object,
    required: true,
  },
});
const data = reactive({
  form: {},
  visible: false,
  loading: false,
  formRef: null,
  rules: {
    name: [{ required: true, message: "请输入品牌名称" }],
    alias: [{ required: true, message: "请输入品牌中文名" }],
    type: [{ required: true, message: "请选择品牌类型", trigger: "change" }],
    img: [{ required: true, message: "请上传品牌logo", trigger: "change" }],
  },
  load: false,
});
const { rules, form, visible, loading, formRef, load } = toRefs(data);
const open = async (id) => {
  if (!load.value) {
    load.value = true;
    await 1;
  }
  if (id) getDetail(id);
  else form.value = { status: 1 };
  visible.value = true;
};
const getDetail = (id) => {
  api.getDetail({ id }).then((res) => {
    form.value = res.data;
    form.value.customs_type = form.value.customs_type || "";
  });
};
const save = () => {
  if (!formRef.value) return;
  formRef.value.validate((valid) => {
    if (valid) {
      loading.value = true;
      if (form.value.id) {
        api
          .update(form.value)
          .then((res) => {
            ElMessage.success(res.message);
            emits("query");
            close();
          })
          .finally(() => {
            loading.value = false;
          });
      } else {
        api
          .add(form.value)
          .then((res) => {
            ElMessage.success(res.message);
            emits("query");
            close();
          })
          .finally(() => {
            loading.value = false;
          });
      }
    }
  });
};
const initForm = () => {
  form.value = getForm();
};
// 初始化
const getForm = () => {
  return {
    name: "",
    status: 1,
    alias: "",
    type: null,
    img: [],
    url: null,
    desc: "",
  };
};

onMounted(() => {
  initForm();
});
const close = () => {
  initForm();
  visible.value = false;
};
defineExpose({ open });
</script>
